<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title data-i18n="resources.title_graphThemeLayer"></title>
    <script type="text/javascript" src="../../dist/maplibregl/include-maplibregl.js"></script>
    <script type="text/javascript" src="../data/chinaConsumptionLevel.js"></script>
    <style>
        body, #map {
            position: absolute;
            width: 100%;
            height: 100%
        }

        .maplibregl-popup-close-button {
          outline: none;
        }

        .editPane {
            position: absolute;
            right: 65px;
            top: 8px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
            border-radius: 4px;
        }

        .tooltip-inner {
            width: 68px;
            background-color: transparent;
            color: #515151;
        }

        .graph {
            margin: 5px;
            width: 26px;
            height: 26px;
            border: none;
            border-radius: 4px;
            background-size: 100%;
        }

        #bar {
            background-image: url("../img/bar.png");
        }

        #bar3d {
            background-image: url("../img/bar3D.png");
        }

        #ling {
            background-image: url("../img/ling.png");
        }

        #point {
            background-image: url("../img/point.png");
        }

        #pie {
            background-image: url("../img/pie.png");
        }

        #ring {
            background-image: url("../img/ring.png");
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div class="editPane" role="group" aria-label="...">
    <button type='button' class='btn btn-default graph active' id='bar' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphBar"></button>
    <button type='button' class='btn btn-default graph' id='bar3d' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphBar3D"></button>
    <button type='button' class='btn btn-default graph' id='ling' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphLine"></button>
    <button type='button' class='btn btn-default graph' id='point' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphPoint"></button>
    <button type='button' class='btn btn-default graph' id='pie' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphPie"></button>
    <button type='button' class='btn btn-default graph' id='ring' data-toggle='tooltip' data-placement='bottom'
            data-i18n="[title]resources.title_GraphRing"></button>
</div>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
<script type="text/javascript">
    var themeLayer, popup,
        host = window.isLocal ? window.server : "https://iserver.supermap.io",
        url = host + "/iserver/services/map-china/rest/maps/China_4326";
    var attribution = "<a href='https://maplibre.org/' target='_blank'>© MapLibre </a>" +
            " with <span>© <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
            " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
    var map = new maplibregl.Map({
        container: 'map',
        style: {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "attribution": attribution,
                    "type": "raster",
                    "tiles": [url + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
                    "tileSize": 256,
                },
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        },
        center: [105.85, 36.79],
        zoom: 4
    });
    map.addControl(new maplibregl.NavigationControl(), 'top-left');
    map.addControl(new maplibregl.supermap.LogoControl({ link: "https://iclient.supermap.io" }), 'bottom-right');

    bindEvent();
    initFeaterDatasAddStyles();
    createBarThemeLayer();

    function bindEvent() {
        $("[data-toggle='tooltip']").on("mouseover", function () {
            $("[data-toggle='tooltip']").tooltip();
        });

        $("[data-toggle='tooltip']").on("mouseout", function () {
            $("[data-toggle='tooltip']").tooltip("hide");
        });

        $(".graph").on("click", function () {
            $(".graph").removeClass("active");
        });

        $("#bar").on("click", function () {
            $("#bar").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createBarThemeLayer();
        });
        $("#bar3d").on("click", function () {
            $("#bar3d").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createBar3DThemeLayer();
        });
        $("#ling").on("click", function () {
            $("#ling").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createLineThemeLayer();
        });
        $("#point").on("click", function () {
            $("#point").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createPointThemeLayer();
        });
        $("#pie").on("click", function () {
            $("#pie").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createPieThemeLayer();
        });
        $("#ring").on("click", function () {
            $("#ring").addClass("active");
            initFeaterDatasAddStyles();
            clearThemeLayer();
            createRingThemeLayer();
        });
    }

    var features,
        chartsSettingForBarAddBar3DCommon,
        chartsSettingForPointOrLine,
        chartsSettingForPieOrRing,
        themeLayerOptions;

    function initFeaterDatasAddStyles() {
        //创建附着要素
//        features = [];
        var feas = [];
        for (var i = 0, len = chinaConsumptionLevel.length; i < len; i++) {
            // 省居民消费水平（单位：元）信息
            var provinceInfo = chinaConsumptionLevel[i];
            //支持传入 maplibregl.supermap.ThemeFeature 类型：
            /* var geo = new maplibregl.LngLat(provinceInfo[1], provinceInfo[2]);
             var attrs = {};
             attrs.NAME = provinceInfo[0];
             attrs.CON2009 = provinceInfo[3];
             attrs.CON2010 = provinceInfo[4];
             attrs.CON2011 = provinceInfo[5];
             attrs.CON2012 = provinceInfo[6];
             attrs.CON2013 = provinceInfo[7];
             var fea = new maplibregl.supermap.ThemeFeature(geo, attrs);
             features.push(fea);*/

            //支持 传入 GeoJSON 规范数据类型：
            var fea = {
                "type": "feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [provinceInfo[1], provinceInfo[2]]
                },
                "properties": {
                    "NAME": provinceInfo[0],
                    "CON2009": provinceInfo[3],
                    "CON2010": provinceInfo[4],
                    "CON2011": provinceInfo[5],
                    "CON2012": provinceInfo[6],
                    "CON2013": provinceInfo[7],
                }
            };

            feas.push(fea);
        }

        features = {
            "type": "FeatureCollection",
            "features": feas
        };

        //Bar add Bar3D chartsSetting
        chartsSettingForBarAddBar3DCommon = {
            width: 260,
            height: 120,
            codomain: [0, 40000],
            xShapeBlank: [15, 15, 15],
            axisYTick: 4,
            axisYLabels: ["4万", "3万", "2万", "1万", "0"],
            axisXLabels: ["09年", "10年", "11年", "12年", "13年"],
            backgroundRadius: [5, 5, 5, 5],
            backgroundStyle: {
                fillColor: "#d1eeee",
                shadowBlur: 12,
                shadowColor: "#d1eeee"
            }
        };

        //Point add Line chartsSetting
        chartsSettingForPointOrLine = {
            width: 220,
            height: 100,
            codomain: [0, 40000],
            xShapeBlank: [10, 10],
            axisYTick: 4,
            axisYLabels: ["4万", "3万", "2万", "1万", "0"],
            axisXLabels: ["09年", "10年", "11年", "12年", "13年"],
            backgroundStyle: {fillColor: "#d1eeee"},
            backgroundRadius: [5, 5, 5, 5],
            useXReferenceLine: true,
            pointStyle: {
                pointRadius: 5,
                shadowBlur: 12,
                shadowColor: "#D8361B",
                fillOpacity: 0.8
            },
            pointHoverStyle: {
                stroke: true,
                strokeColor: "#D8361B",
                strokeWidth: 2,
                fillColor: "#ffffff",
                pointRadius: 4
            },
        };

        //Pie add Ring chartsSetting
        chartsSettingForPieOrRing = {
            width: 240,
            height: 100,
            codomain: [0, 40000],       // 允许图表展示的值域范围，此范围外的数据将不制作图表
            sectorStyle: {fillOpacity: 0.8},      // 柱状图中柱条的（表示字段值的图形）样式
            sectorStyleByFields: [
                {fillColor: "#FFB980"},
                {fillColor: "#5AB1EF"},
                {fillColor: "#B6A2DE"},
                {fillColor: "#2EC7C9"},
                {fillColor: "#D87A80"}],
            sectorHoverStyle: {fillOpacity: 1},
            xShapeBlank: [10, 10, 10],      // 水平方向上的空白间距参数
            axisYLabels: ["4万", "3万", "2万", "1万", "0"],         // y 轴标签内容
            axisXLabels: ["09年", "10年", "11年", "12年", "13年"],         // x 轴标签内容
            backgroundStyle: {fillColor: "#CCE8CF"},        // 背景样式
            backgroundRadius: [5, 5, 5, 5],        // 背景框圆角参数
        };

        //设置graphThemeLayer option参数
        themeLayerOptions = {
//          map: map,//该可选参数将在下个版本遗弃
            attributions: " ",
            themeFields: ["CON2009", "CON2010", "CON2011", "CON2012", "CON2013"],
            opacity: 0.9,
            chartsSetting: {},
        };
    }

    function createBarThemeLayer() {
        var chartsSettingForBar = chartsSettingForBarAddBar3DCommon;
        chartsSettingForBar.barStyle = {fillOpacity: 0.7};      // 柱状图中柱条的（表示字段值的图形）样式
        chartsSettingForBar.barHoverStyle = {fillOpacity: 1};      //  柱条 hover 样式
        //阴影样式
        chartsSettingForBar.barShadowStyle = {
            shadowBlur: 8,
            shadowOffsetX: 2,
            shadowOffsetY: 2,
            shadowColor: "rgba(100,100,100,0.8)"
        };
        chartsSettingForBar.barLinearGradient = [
            ["#00FF00", "#00CD00"],
            ["#00CCFF", "#5E87A2"],
            ["#00FF66", "#669985"],
            ["#CCFF00", "#94A25E"],
            ["#FF9900", "#A2945E"]];

        themeLayerOptions.chartsSetting = chartsSettingForBar;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("BarLayer", "Bar", themeLayerOptions);
        map.addLayer(themeLayer);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
        themeLayer.addFeatures(features);
    }

    //创建Bar3D图表
    function createBar3DThemeLayer() {
        var chartsSettingForBar3D = chartsSettingForBarAddBar3DCommon;
        chartsSettingForBar3D.useXReferenceLine = true;
        chartsSettingForBar3D.xReferenceLineStyle = {strokeColor: "#008acd", strokeOpacity: 0.4};
        // 3d 柱条正面样式（3d 柱条的侧面和顶面会以 3d 柱条正面样式为默认样式）
        chartsSettingForBar3D.barFaceStyle = {stroke: true};
        // 按字段设置 3d 柱条正面样式
        chartsSettingForBar3D.barFaceStyleByFields = [
            {fillColor: "#FFB980"},
            {fillColor: "#5AB1EF"},
            {fillColor: "#B6A2DE"},
            {fillColor: "#2EC7C9"},
            {fillColor: "#D87A80"}];
        // 3d 柱条正面 hover 样式（3d 柱条的侧面和顶面 hover 会以 3d 柱条正面 hover 样式为默认 hover 样式）
        chartsSettingForBar3D.barFaceHoverStyle = {
            stroke: true,
            strokeWidth: 1,
            strokeColor: "#ffff00"
        };

        themeLayerOptions.chartsSetting = chartsSettingForBar3D;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("Bar3DLayer", "Bar3D", themeLayerOptions);
        map.addLayer(themeLayer);
        themeLayer.addFeatures(features);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    //创建Line图表
    function createLineThemeLayer() {
        chartsSettingForPointOrLine.pointStyle.fillColor = "#9966CC";
        themeLayerOptions.chartsSetting = chartsSettingForPointOrLine;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("LineLayer", "Line", themeLayerOptions);
        map.addLayer(themeLayer);
        themeLayer.addFeatures(features);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    //创建Point图表
    function createPointThemeLayer() {
        chartsSettingForPointOrLine.pointStyle.fillColor = "#D8361B";
        themeLayerOptions.chartsSetting = chartsSettingForPointOrLine;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("PointLayer", "Point", themeLayerOptions);
        map.addLayer(themeLayer);
        themeLayer.addFeatures(features);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    //创建Pie图表
    function createPieThemeLayer() {
        themeLayerOptions.chartsSetting = chartsSettingForPieOrRing;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("PieLayer", "Pie", themeLayerOptions);
        map.addLayer(themeLayer);
        themeLayer.addFeatures(features);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    //创建Ring图表
    function createRingThemeLayer() {
        chartsSettingForPieOrRing.innerRingRadius = 20;
        themeLayerOptions.chartsSetting = chartsSettingForPieOrRing;
        themeLayer = new maplibregl.supermap.GraphThemeLayer("RingSymbolLayer", "Ring", themeLayerOptions);
        map.addLayer(themeLayer);
        themeLayer.addFeatures(features);
        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    //清除图表专题图
    function clearThemeLayer() {
        if (themeLayer) {
            themeLayer.clear();
            themeLayer.removeFromMap();
            themeLayer = null;
        }
    }

    function showInfoWin(e) {
        closeInfoWin();
        // e.target 是图形对象，即数据的可视化对象，柱状图中是柱条;
        // 图形对象的 refDataID 属性是数据（feature）的 ID 属性，它指明图形对象是由那个数据制作而来;
        // 图形对象的 dataInfo 属性是图形对象表示的具体数据，他有两个属性，field 和 value;
        if (e.target && e.target.refDataID && e.target.dataInfo) {
            // 获取图形对应的数据 (feature)
            var fea = themeLayer.getFeatureById(e.target.refDataID);

            var info = e.target.dataInfo;

            // 弹窗内容
            var contentHTML = "<div style='color: #000; background-color: #fff'>";
            contentHTML += resources.text_Name + "<br><strong>" + fea.attributes.NAME + "</strong>";

            contentHTML += "<hr style='margin: 3px'>";
            switch (info.field) {
                case "CON2009":
                    contentHTML += resources.text_consumptionLevel1 + "09" + resources.text_consumptionLevel2 + "<br/><strong>" + info.value + "</strong>（" + resources.text_yuan + "）";
                    break;
                case "CON2010":
                    contentHTML += resources.text_consumptionLevel1 + "10" + resources.text_consumptionLevel2 + "<br/><strong>" + info.value + "</strong>（" + resources.text_yuan + "）";
                    break;
                case "CON2011":
                    contentHTML += resources.text_consumptionLevel1 + "11" + resources.text_consumptionLevel2 + "<br/><strong>" + info.value + "</strong>（" + resources.text_yuan + "）";
                    break;
                case "CON2012":
                    contentHTML += resources.text_consumptionLevel1 + "12" + resources.text_consumptionLevel2 + " <br/><strong>" + info.value + "</strong>（" + resources.text_yuan + "）";
                    break;
                case "CON2013":
                    contentHTML += resources.text_consumptionLevel1 + "13" + resources.text_consumptionLevel2 + " <br/><strong>" + info.value + "</strong>（" + resources.text_yuan + "）";
                    break;
                default:
                    contentHTML += "No Data";
            }
            contentHTML += "</div>";

            var tempPoint = map.unproject({x: e.event.x, y: e.event.y});
            popup = new maplibregl.Popup({closeOnClick: false})
                .setLngLat([tempPoint.lng, tempPoint.lat])
                .setHTML(contentHTML)
                .addTo(map);
            return;
        }
        closeInfoWin();
    }

    // 移除地图弹窗
    function closeInfoWin() {
        if (popup) {
            popup.remove(map);
        }
    }

</script>
</body>
</html>
